package com.ict.web.controller.yzq;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.core.page.TableDataInfo;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.poi.ExcelUtil;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.domain.ProjectInfo;
import com.ict.web.dto.ExportProjectInfo;
import com.ict.web.dto.ImportProjectInfoDto;
import com.ict.web.service.ProjectInfoService;
import com.ict.web.vo.ProjectInfoVo;
import com.ict.web.vo.req.GetProjectInfoListReq;
import com.ict.web.vo.req.SaveProjectInfoReq;
import com.ict.web.vo.req.UpdateProjectInfoReq;
import com.ict.web.vo.resp.GetProjectInfoListResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/projectInfo")
@Api(tags = "项目信息控制器")
public class ProjectInfoController extends BaseController {
    @Autowired
    private ProjectInfoService projectInfoService;

    @PostMapping("/saveProjectInfo")
    @ApiOperation("新增项目信息")
    public AjaxResult saveProjectInfo(@RequestBody SaveProjectInfoReq req) {
        return projectInfoService.saveProjectInfo(req);
    }

    @DeleteMapping("/deletePorjectInfo")
    @ApiOperation("删除项目信息")
    public AjaxResult deletePorjectInfo(@RequestParam("proId") String proId) {
        return projectInfoService.deletePorjectInfo(proId);
    }

    @PutMapping("/updatePorjectInfo")
    @ApiOperation("修改项目信息")
    public AjaxResult updatePorjectInfo(@RequestBody UpdateProjectInfoReq req) {
        return projectInfoService.updatePorjectInfo(req);
    }

    @PostMapping("/getProjectInfoList")
    @ApiOperation("获取项目信息列表")
    public TableDataInfo getProjectInfoList(@RequestBody GetProjectInfoListReq req) {
        Page<Object> page = PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<GetProjectInfoListResp> resps = projectInfoService.getProjectInfoList(req);
        TableDataInfo info = getDataTable(resps);
        info.setTotal(page.getTotal());
        return info;
    }


    @PostMapping("/importProjectInfo")
    @ApiOperation("导入项目信息")
    public AjaxResult importProjectInfo(MultipartFile file) throws Exception{
        ExcelUtil<ImportProjectInfoDto> util = new ExcelUtil<>(ImportProjectInfoDto.class);
        List<ImportProjectInfoDto> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(projectInfoService.saveProjectInfoList(list));
    }

    @PostMapping("/exportProjectInfo")
    @ApiOperation("导出")
    public AjaxResult exportProjectInfo(HttpServletResponse response,@RequestBody GetProjectInfoListReq req) throws UnsupportedEncodingException {
        List<ProjectInfoVo> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(req.getIds())){
            List<ExportProjectInfo> idsList = projectInfoService.getProjectInfoListByIds(req.getIds());
            idsList.stream().forEach(s->{
                ProjectInfoVo projectInfo = new ProjectInfoVo();
                BeanUtils.copyProperties(s,projectInfo);
                if(StringUtils.isNotNull(s.getProTime())){
                    LocalDateTime updateTime = s.getProTime();
                    String replace = String.valueOf(updateTime).replace('T', ' ');
                    projectInfo.setTime(replace);
                }
                if(StringUtils.isNotEmpty(projectInfo.getProInvestmentEstimation())){
                    String decimal = new BigDecimal(projectInfo.getProInvestmentEstimation()).stripTrailingZeros().toPlainString();
                    projectInfo.setProInvestmentEstimation(decimal);
                }
                list.add(projectInfo);
            });
        }else {
            List<GetProjectInfoListResp> resps = projectInfoService.getProjectInfoAll();
            resps.stream().forEach(s->{
                ProjectInfoVo projectInfo = new ProjectInfoVo();
                ProjectInfo projectInfoResp = s.getProjectInfo();
                BeanUtils.copyProperties(projectInfoResp,projectInfo);
                if(StringUtils.isNotNull(projectInfoResp.getProTime())){
                    LocalDateTime updateTime = projectInfoResp.getProTime();
                    String replace = String.valueOf(updateTime).replace('T', ' ');
                    projectInfo.setTime(replace);
                }
                if(StringUtils.isNotEmpty(projectInfo.getProInvestmentEstimation())){
                    String decimal = new BigDecimal(projectInfo.getProInvestmentEstimation()).stripTrailingZeros().toPlainString();
                    projectInfo.setProInvestmentEstimation(decimal);
                }
                list.add(projectInfo);
            });
        }
        ExcelUtil<ProjectInfoVo> util = new ExcelUtil<>(ProjectInfoVo.class);
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("项目明细报表导出.xlsx", "UTF-8"));
        util.exportExcel(response,list,"项目明细");
        return AjaxResult.success();
    }

    /**
     * 初设方案上传文件
     */
//    @PreAuthorize("@ss.hasPermi('web:projectInfo:upload')")
    @ApiOperation(value = "上传专家审核结论", httpMethod = "POST")
    @PostMapping(value = "/upload/{proId}")
    public AjaxResult upload(@Validated @RequestBody YzqFile file, @PathVariable Long proId) {
        return toAjax(projectInfoService.upload(file, proId));
    }
}
